package com.skivingcloud.mybatis.flex.codegen;

import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import com.mybatisflex.core.util.StringUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 前端代码生成
 * @author hushouquan
 */
public class VueElementTsGenerator implements IGenerator{
    
    private String templatePath = "./templates/vueElementTs.tpl";
    private final boolean isOverwriteEnable = true;
    
    @Override
    public String getTemplatePath(){
        return this.templatePath;
    }
    
    @Override
    public void setTemplatePath(String templatePath){
        this.templatePath = templatePath;
    }
    
    @Override
    public void generate(Table table, GlobalConfig globalConfig){
        PackageConfig packageConfig = globalConfig.getPackageConfig();
        File vueElementTsFile = new File(packageConfig.getMapperXmlPath().replaceAll("mapping", "vue") + "/" + StringUtil.firstCharToLowerCase(table.buildEntityClassName()) + ".vue");
        if (!vueElementTsFile.exists() || isOverwriteEnable) {
            Map<String, Object> params = new HashMap<>(4);
            params.put("table", table);
            params.put("packageConfig", packageConfig);
            globalConfig.getTemplateConfig().getTemplate().generate(params, this.templatePath, vueElementTsFile);
            System.out.println("Vue ---> " + vueElementTsFile);
        }
    }
}
